package com.example.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.example.common.entity.SimpleVideoEntity
import com.example.home.cache.HomeItemCache
import com.example.home.cache.RecycleCachePool
import com.example.home.databinding.ItemVideoLayoutBinding
import com.example.image.glide.GlideImgLoader
import kotlin.math.max

/**
 * 适配器
 */
class HomeVideoListAdapter constructor(private val list:MutableList<SimpleVideoEntity>) :RecyclerView.Adapter<HomeVideoListAdapter.ItemViewHolder>(){

    private var scrollState = RecyclerView.SCROLL_STATE_IDLE //滚动停止
    var preloadEndCount =0//倒数第几个出发预加载
    private var isPreloading =false//是否预加载中
    var preloadMoreData:(()->Unit)? = null//回调方法

    var itemViewCache: HomeItemCache?= null
    private var itemClickListener: OnItemClickListener ?= null

    fun setOnItemClickListener(l: OnItemClickListener){
        itemClickListener = l
    }
    inner class ItemViewHolder(val dataBinding: ItemVideoLayoutBinding) : RecyclerView.ViewHolder(dataBinding.root)
    //点击事件接口
    interface OnItemClickListener{
        fun onItemChick(v: View, position: Int)
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val itemViewHolder = ItemViewHolder(ItemVideoLayoutBinding.inflate(LayoutInflater.from(parent.context), parent, false))
        itemViewCache?.addViewHolder(viewType, itemViewHolder)
        return itemViewHolder
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val entity = list[position]
        //视频标题和描述
        holder.dataBinding.tvTitle.text = entity.title
        holder.dataBinding.tvDesc.text = entity.description

        GlideImgLoader.loadCornerImage(holder.dataBinding.root.context, entity.videomainimag, holder.dataBinding.ivVideoThumbnail,20 )
        GlideImgLoader.loadCircleImage(holder.dataBinding.root.context, entity.avatar_url, holder.dataBinding.ivAvatar)

//播放按钮点击事件
        holder.dataBinding.ivVideoPlay.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }

        //评论按钮点击事件
        holder.dataBinding.ivCommend.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }

        //更多按钮点击事件
        holder.dataBinding.ivMore.setOnClickListener {
            itemClickListener?.onItemChick(it, position)
        }
        //检查是否需要加载下一页数据
        checkPreloadMoreData(position)

    }

    override fun getItemCount(): Int =list.size

    override fun getItemViewType(position: Int): Int {
        return RecycleCachePool.HOME_LIST_ITEM_VIEW_TYPE
    }

    override fun getItemId(position: Int): Long = position.toLong()



    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        if (itemViewCache != null){
            recyclerView.setViewCacheExtension(itemViewCache)
        }

        //添加滚动监听事件
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                scrollState = newState
                super.onScrollStateChanged(recyclerView, newState)
            }
        })
    }

    override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
        super.onDetachedFromRecyclerView(recyclerView)
        itemViewCache?.clearCache()
    }



    /**
     * 数据预加载
     */
    private fun checkPreloadMoreData(position: Int){
        if (scrollState != RecyclerView.SCROLL_STATE_IDLE //滚动中触发
            && position == max(list.size - 1 - preloadEndCount, 0) //当前页面最后第preloadEndCount个触发
            && !isPreloading){
            //正在触发加载标识
            isPreloading = true
            //执行回调方法
            preloadMoreData?.invoke()
        } else {
            isPreloading = false
        }
    }


}